﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using eduscrum.DAL;
using eduscrum.Models;
using eduscrum.ViewModels;

namespace eduscrum.Controllers
{ 
    public class TaskController : Controller
    {/* Tasks will be avaiable in a later iteration
        private SystemContext db = new SystemContext();

        //
        // GET: /Task/

        public ViewResult Index()
        {
            var tasks = db.Tasks.ToList();
            List<TaskVM> tasksVM = AutoMapper.Mapper.Map<List<Task>, List<TaskVM>>(tasks);         
            return View(tasksVM);
        }

        //
        // GET: /Task/Details/5

        public ViewResult Details(int id)
        {
            Task task = db.Tasks.Find(id);
            TaskVM taskVM = new TaskVM();
            AutoMapper.Mapper.Map(task, taskVM);
            return View(taskVM);
        }

        //
        // GET: /Task/Create

        public ActionResult Create()
        {
            var viewModel = new TaskVM();
            return View("Create", viewModel);
        } 

        //
        // POST: /Task/Create

        [HttpPost]
        public ActionResult Create(TaskVM taskToCreate, int userStoryID)
        {
            if (ModelState.IsValid)
            { 
                UserStory userStoryToAddTaskTo = db.UserStories.Find(userStoryID);
                Task newTask = new Task();
                AutoMapper.Mapper.Map(taskToCreate, newTask);
                userStoryToAddTaskTo.Tasks.Add(newTask);
                db.Entry(userStoryToAddTaskTo).State = EntityState.Modified;
                db.SaveChanges();
            }
            return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "UserStory", action = "Edit", Id = userStoryID }));
        }
        
        //
        // GET: /Task/Edit/5
 
        public ActionResult Edit(int id)
        {
            Task taskToEdit = db.Tasks.Find(id);
            TaskVM viewModel = new TaskVM();
            AutoMapper.Mapper.Map(taskToEdit, viewModel);
            return View("Edit", viewModel);
        }

        //
        // POST: /Task/Edit/5

        [HttpPost]
        public ActionResult Edit(TaskVM taskToEditVM, int userStoryID)
        {
            if (ModelState.IsValid)
            {
                Task taskEdited = new Task();
                AutoMapper.Mapper.Map(taskToEditVM, taskEdited);
                db.Entry(taskEdited).State = EntityState.Modified;
                db.SaveChanges();
            }
            return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "UserStory", action = "Edit", Id = userStoryID }));
        }

        //
        // GET: /Task/Delete/5
 
        public ActionResult Delete(int id)
        {
            Task task = db.Tasks.Find(id);
            int userStoryID = task.UserStory.UserStoryID;
            db.Tasks.Remove(task);
            db.SaveChanges();
            return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "UserStory", action = "Edit", Id = userStoryID }));
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }*/
    }
}